home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / addpcx.zip / CGA.C < prev    next >
C/C++ Source or Header  |  1988-04-17  |  4KB  |  236 lines

  1. /*    CGA.c -  Low level drivers for the CGA board
  2. */
  3.  
  4.  
  5. #include "lib.h"
  6. #include "vgr.h"
  7.  
  8. int cga_init();
  9. int cga_write_row();
  10. int cga_clear();
  11. int cga_clr_point();
  12. int cga_xor_point();
  13. int cga_get_point();
  14. int cga_set_point();
  15. int cga_mode();
  16. int cga_null();
  17. int cga_palette(); /* not tested! */
  18. int cga_movmem();
  19. int cga_peekb();
  20. int cga_pokeb();
  21.  
  22. static int mode;
  23. static int (*cga_func[])() = {
  24.     cga_init,       cga_clear,     cga_set_point, cga_clr_point,
  25.     cga_xor_point,  cga_get_point, cga_write_row, cga_null,
  26.     cga_null,       cga_mode,      cga_movmem, cga_peekb, cga_pokeb,
  27.     cga_null,       cga_palette    };
  28.  
  29.  
  30. static unsigned char far * far *cga_column;
  31.  
  32.  
  33. static int cga_null()
  34. {
  35.     return ERROR;
  36. }
  37.  
  38.  
  39. int cga_peekb( p )
  40. unsigned char far *p;
  41. {
  42. #asm
  43.     les   bx, dword ptr 4[bp]
  44.     mov   dx, 03dah
  45. x1:    in    al, dx
  46.     test  al, 1
  47.     jnz   x1
  48. x2:    in    al, dx
  49.     test  al, 1
  50.     jz    x2
  51.     mov   al, es:byte ptr [bx]
  52.     sub ah,ah
  53. #endasm
  54. }
  55.  
  56.  
  57. int cga_pokeb( p, b )
  58. unsigned char far *p;
  59. unsigned char b;
  60. {
  61. #asm
  62.     mov   ah,byte ptr 8[bp]
  63.     les   bx,dword ptr 4[bp]
  64.     mov   dx, 03dah
  65. y1:    in    al, dx
  66.     test  al, 1
  67.     jnz   y1
  68. y2:    in    al, dx
  69.     test  al, 1
  70.     jz    y2
  71.     mov   es:byte ptr [bx],ah
  72. #endasm
  73. }
  74.  
  75.  
  76. /*    WARNING: cga_movmem() doesn't check for overlapping blocks!!!!
  77. */
  78.  
  79. int cga_movmem( s, d, n )
  80. unsigned char far *s, *d;
  81. int n;
  82. {
  83.     for ( ; n--; s++, d++ )
  84.     {
  85. #asm
  86.     les   bx, dword ptr 4[bp]
  87.     mov   ah, es:byte ptr [bx]
  88.     les   bx, dword ptr 8[bp]
  89.     mov   dx, 03dah
  90. z1:    in    al, dx
  91.     test  al, 1
  92.     jnz   z1
  93. z2:    in    al, dx
  94.     test  al, 1
  95.     jz    z2
  96.     mov   es:byte ptr [bx],ah
  97. #endasm
  98.     };
  99. }
  100.  
  101. int cga_init()
  102. {
  103.     void *malloc();
  104.     int row;
  105.  
  106.     if ( !cga_column )
  107.        cga_column = CASTUCFPP malloc( sizeof CASTUCFP * 200 );
  108.  
  109.     if ( !cga_column )
  110.        return ERROR;
  111.  
  112.     for ( row = 0; row < 200; row++ )
  113.        cga_column[row] = CASTUCFP BASE_CGA + ((uint)row >> 1)*80l + (row & 1)*0x2000;
  114.  
  115.     VGR_NBPL = VGR_NCOLORS = 
  116.     VGR_HRES = VGR_VRES = 0;   /* must set mode first! */
  117.  
  118.     movmem( cga_func, vgr_func, sizeof(vgr_func) );
  119.     return OK;
  120. }
  121.  
  122.  
  123. /*    Warning: NOT TESTED
  124. */
  125. int cga_palette( bg, fg )
  126. int fg, bg;
  127. {
  128.     if ( !mode )
  129.        return OK;  /* no meaning if not in 320x200x4 mode */
  130.  
  131.     outportb( 0x3d9, ((uint)(fg & 0x07)<<5) | (bg & 0x0f) );
  132.     return OK;
  133. }
  134.  
  135.  
  136. int cga_write_row( row, prow, nbytes )
  137. register unsigned int nbytes;
  138. unsigned char far *prow;
  139. int row;
  140. {
  141.     cga_movmem( prow, cga_column[row], nbytes );
  142. }
  143.  
  144.  
  145. int cga_clear()
  146. {
  147.     int i;
  148.  
  149.     for ( i=0; i < 0x4000; i++ )
  150.        cga_pokeb( CASTUCFP (BASE_CGA + (long)i), 0 );
  151. }
  152.  
  153.  
  154. int cga_clr_point( x, y )
  155. unsigned int x, y;
  156. {
  157.     unsigned char far *p;
  158.  
  159.     if ( mode )
  160.     {  p = cga_column[y] + (x>>2);
  161.        cga_pokeb( p, cga_peekb(p) & ~(0xc0 >> ((x & 0x03) << 1)) );
  162.     } else 
  163.     {  p = cga_column[y] + (x>>3);
  164.        cga_pokeb( p, cga_peekb(p) & ~(0x80 >> (x & 0x07)) );
  165.     };
  166. }
  167.  
  168.  
  169. int cga_xor_point( x, y, color )
  170. unsigned int x, y, color;
  171. {
  172.     cga_set_point( x, y, cga_get_point( x, y ) ^ color );
  173. }
  174.  
  175.  
  176. int cga_get_point( x, y )
  177. unsigned int x, y;
  178. {
  179.     uint i;
  180.  
  181.     if ( mode )
  182.     {  i = (unsigned int)(x & 0x03) << 1;
  183.        return (uint)(cga_peekb(cga_column[y]+(x>>2))&(0xc0>>i)) >> (6-i);
  184.     } else 
  185.        return !!( cga_peekb( cga_column[y] + (x>>3) ) 
  186.                                        & (0x80 >> (x & 0x07)));
  187. }
  188.  
  189.  
  190. int cga_set_point( x, y, color )
  191. unsigned int x, y, color;
  192. {
  193.     unsigned char far *p;
  194.     unsigned char i;
  195.  
  196.     if ( !mode )
  197.     {  p = cga_column[y] + (x>>3);
  198.        return cga_pokeb( p, cga_peekb(p) | (0x80 >> (x & 0x07)) );
  199.     };
  200.  
  201.     p = cga_column[y] + (x>>2);
  202.     i = (x & 3) << 1;
  203.     cga_pokeb( p, cga_peekb(p) & ~(0xc0 >> i) );
  204.     cga_pokeb( p, cga_peekb(p) | ((color & 0x03) << (6-i)) );
  205. }
  206.  
  207.  
  208. int cga_mode( m )
  209. int m;
  210. {
  211.     if ( m == MODE_TEXT0 )
  212.        return vgr_mode(3);
  213.  
  214.     if ( m == MODE_APA3 )   /* 320x200x4 */
  215.     {  VGR_HRES = 320;
  216.        VGR_VRES = 200;
  217.        VGR_NCOLORS = 4;
  218.        VGR_NBPL =  80;
  219.        mode = 1;
  220.        return vgr_mode(4);
  221.     };
  222.  
  223.     if ( m == MODE_APA2 )   /* 640x200x2 */
  224.     {  VGR_HRES = 640;
  225.        VGR_VRES = 200;
  226.        VGR_NCOLORS = 2;
  227.        VGR_NBPL =  80;
  228.        mode = 0;
  229.        return vgr_mode(6);
  230.     };
  231.  
  232.     return ERROR;  /* mode not currently supported */
  233. }
  234.  
  235.  
  236.